﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Oxite.Infrastructure;
using ECube.SEC.Models;
namespace ECube.SEC.Repositories.SqlServer
{
    public class SqlServerAuthorizeRepository : IAuthorizeRepository
    {
        private readonly ECubeAuthorizeDataContext context;
        private readonly Guid siteID;

        public SqlServerAuthorizeRepository(ECubeAuthorizeDataContext context, OxiteContext oxiteContext)
        {
            this.context = context;
            siteID = oxiteContext.Site.ID;
        }

        #region IAuthorizeRepository 成员

        public List<Permission> GetPermissions()
        {
            return (from p in context.ecube_SEC_Permission
                    select new Permission(p.PermissionId,p.ParentId,p.PermissionKey,p.ModuleId,p.Name,p.Descriptor)
                    ).ToList<Permission>();            
        }

        
        public List<string> GetPermissionsForUser(User user)
        {
            List<string> list =(from p in context.ecube_SEC_Authorization
                where p.SiteId == siteID && p.UserId == user.ID.ToString() 
                select p.PermissionKey).ToList<string>();
            return list;
        }

        #endregion
    }
}
